import 'package:flutter/material.dart';
import 'package:play_android/com/zhihaoliang/base/BaseState.dart';
import 'package:play_android/com/zhihaoliang/const/AppStr.dart';
import 'package:play_android/com/zhihaoliang/http/HttpException.dart';
import 'package:play_android/com/zhihaoliang/play/find/model/TreeClass.dart';
import 'package:play_android/com/zhihaoliang/play/find/model/TreeItem.dart';
import 'package:play_android/com/zhihaoliang/play/presenter/TreePresenter.dart';
import 'package:play_android/com/zhihaoliang/widget/NoData.dart';
import 'ArticlesPage.dart';

///  ***************************************************************************
///  中间"发现"的页面
///  ***************************************************************************

class FindPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _FindPageState();
  }
}

class _FindPageState extends State<FindPage>
    with BaseState
    implements TreeCallBack {
  List<TreeClass> _list;

  @override
  void initState() {
    super.initState();
    new TreePresenter(this).load();
  }

  @override
  Widget build(BuildContext context) {
    if (_list == null) {
      return Center(
        child: CircularProgressIndicator(),
      );
    }

    if (_list.length == 0) {
      return Center(
        child: NoData(),
      );
    }

    Widget listView = ListView.builder(
      itemCount: _list.length,
      itemBuilder: (context, postion) => buildItem(_list[postion]),
    );

    return listView;
  }

  @override
  void onLoadTreeComplete(List<TreeClass> list) {
    setState(() {
      _list = list;
    });
  }

  @override
  void onLoadTreeError(HttpException httpException) {
    onTipError(httpException, context, AppStr.LOAD_TREE_FAIL);
  }

  buildItem(TreeClass item) {
    Text name = Text(
      item.name,
      softWrap: true,
      style: TextStyle(
          fontSize: 16.0, color: Colors.black, fontWeight: FontWeight.bold),
      textAlign: TextAlign.left,
    );

    List<TreeItem> list = item.children;

    StringBuffer strContent = StringBuffer();

    list.forEach((treeItem) {
      strContent.write("${treeItem.name}   ");
    });

    Text content = Text(
      strContent.toString(),
      softWrap: true,
      style: TextStyle(color: Colors.black),
      textAlign: TextAlign.left,
    );

    return Card(
      elevation: 4.0,
      child: InkWell(
        onTap: () {
          _handOnItemClick(item);
        },
        child: Container(
          padding: EdgeInsets.all(15.0),
          child: Row(
            children: <Widget>[
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      padding: const EdgeInsets.only(bottom: 8.0),
                      child: name,
                    ),
                    content,
                  ],
                ),
              ),
              Icon(
                Icons.chevron_right,
                color: Colors.black,
              ),
            ],
          ),
        ),
      ),
    );
  }

  ///点击操作
  void _handOnItemClick(TreeClass itemData) {
    Navigator.of(context)
        .push(MaterialPageRoute(builder: ((context) => ArticlesPage(itemData))));
  }
}
